﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Infragistics.Win.UltraWinListView;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public class HCheckListBox : UltraListView
    {
        #region DataBinding

        /// <summary></summary>
        private string _displayMember = "";
        /// <summary></summary>
        public string DisplayMember { get { return _displayMember; } set { _displayMember = value; } }

        /// <summary></summary>
        private string _valueMember = "";
        /// <summary></summary>
        public string ValueMember { get { return _valueMember; } set { _valueMember = value; } }

        /// <summary></summary>
        private object[] _dataSource;
        /// <summary></summary>
        public object[] DataSource { set { _dataSource = value; } }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataSource"></param>
        public void SetDataSource(Dictionary<int, string> dataSource)
        {
            if (dataSource.Count == 0) return;

            _displayMember = "Value";
            _valueMember = "Key";

            this.Items.Clear();
            foreach (int id in dataSource.Keys)
            {
                UltraListViewItem item = new UltraListViewItem(id.ToString());
                item.Value = dataSource[id];
                this.Items.Add(item);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateBindings()
        {
            foreach (Binding binding in this.DataBindings)
            {
                binding.WriteValue();
            }
        }

        /// <summary></summary>
        private string _checkedKeys;
        /// <summary></summary>
        public string CheckedKeys
        {
            get { return _checkedKeys; }
            set
            {
                _loading = true;
                _checkedKeys = value;
                if (_checkedKeys != null)
                {
                    string[] keys = _checkedKeys.Split(',');
                    foreach (UltraListViewItem item in this.Items)
                    {
                        item.CheckState = keys.Contains(item.Key) ? CheckState.Checked : CheckState.Unchecked;
                    }
                }
                _loading = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        public void DataBind(object obj, string propertyName)
        {
            this.DataBindings.Clear();
            this.DataBindings.Add("CheckedKeys", obj, propertyName, true, DataSourceUpdateMode.Never);
        }

        #endregion DataBinding

        /// <summary></summary>
        bool _loading = false;

        /// <summary>
        /// 
        /// </summary>
        public HCheckListBox()
        {
            this.View = UltraListViewStyle.List;
            this.Appearance.BackColor = Color.Transparent;
            this.ViewSettingsList.CheckBoxStyle = CheckBoxStyle.CheckBox;
            this.ViewSettingsList.ImageSize = Size.Empty;
            this.ItemSettings.SelectionType = SelectionType.None;
            this.ItemSettings.HideSelection = true;

            this.ItemCheckStateChanged += new ItemCheckStateChangedEventHandler(HCheckListBox_ItemCheckStateChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="checkState"></param>
        public void SetAll(CheckState checkState)
        {
            _loading = true;
            this.Items.SetCheckState(checkState);
            _loading = false;

            HCheckListBox_ItemCheckStateChanged(this, null);
        }



        /// <summary>
        /// 
        /// </summary>
        public void SetNextValueState()
        {
            int checkedItemsCount = this.CheckedItems.Count;
            int itemsCount = this.Items.Count;

            if (checkedItemsCount < itemsCount)
            {
                this.SetAll(CheckState.Checked);
                return;
            }
            if (checkedItemsCount == itemsCount)
            {
                this.SetAll(CheckState.Unchecked);
                return;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HCheckListBox_ItemCheckStateChanged(object sender, ItemCheckStateChangedEventArgs e)
        {
            if (_loading) return;

            string checkedIndexes = "";

            foreach (UltraListViewItem item in this.CheckedItems)
            {
                checkedIndexes += item.Key + ",";
            }
            if (checkedIndexes.Length > 0)
                checkedIndexes = checkedIndexes.Remove(checkedIndexes.Length - 1, 1);

            CheckedKeys = checkedIndexes;

            UpdateBindings();
        }

    }
}
